package cn.wsalix.shop.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import cn.anlaser.repository.CommonRepository;
import cn.wsalix.shop.entity.Product;
import cn.wsalix.shop.entity.RelationProduct;
import cn.wsalix.shop.type.ProductTypeEnum;

public interface RelationProductRepository extends
		CommonRepository<RelationProduct> {
	@Query("select product from RelationProduct relaProduct,Product product where relaProduct.product.id=product.id and relaProduct.fkId=:fkId and relaProduct.productType = :productType")
	public List<Product> findByTypeAndFkid(@Param("fkId") Long fkId,
			@Param("productType") ProductTypeEnum productType);
	
	@Query("select product from RelationProduct relaProduct,Product product where relaProduct.product.id=product.id and product.parentCat.id=:catId and relaProduct.fkId=:fkId and relaProduct.productType = :productType")
	public List<Product> findByTypeAndFkidAndCat(@Param("fkId") Long fkId,@Param("catId") Long catId,
			@Param("productType") ProductTypeEnum productType);

	@Modifying(clearAutomatically = true)
	@Query("delete from RelationProduct relaProduct where relaProduct.fkId=:fkId and relaProduct.product.id=:productId and relaProduct.productType = :productType")
	public void deleteBy(@Param("fkId") Long fkId, @Param("productId") Long productId, @Param("productType") ProductTypeEnum productType);
}
